using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class UserGiftCardFaceValueDal
	{
		public bool Exists(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from MBF_UserGiftCardFaceValue");
			stringBuilder.Append(" where ");
			stringBuilder.Append("id = @id ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public long Add(UserGiftCardFaceValueModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserGiftCardFaceValue(");
			stringBuilder.Append("FestivalName,FaceValue,DiscountRate");
			stringBuilder.Append(") values (");
			stringBuilder.Append("@FestivalName,@FaceValue,@DiscountRate");
			stringBuilder.Append(") ");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@FestivalName", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@DiscountRate", SqlDbType.Int, 4)
			};
			array[0].Value = model.FestivalName;
			array[1].Value = model.FaceValue;
			array[2].Value = model.DiscountRate;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			long result;
			if (single == null)
			{
				result = 0L;
			}
			else
			{
				result = System.Convert.ToInt64(single);
			}
			return result;
		}
		public bool Update(UserGiftCardFaceValueModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update MBF_UserGiftCardFaceValue set ");
			stringBuilder.Append(" FestivalName = @FestivalName , ");
			stringBuilder.Append(" FaceValue = @FaceValue , ");
			stringBuilder.Append(" DiscountRate = @DiscountRate  ");
			stringBuilder.Append(" where id=@id ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt, 8),
				new SqlParameter("@FestivalName", SqlDbType.NVarChar, 50),
				new SqlParameter("@FaceValue", SqlDbType.Decimal, 9),
				new SqlParameter("@DiscountRate", SqlDbType.Int, 4)
			};
			array[0].Value = model.id;
			array[1].Value = model.FestivalName;
			array[2].Value = model.FaceValue;
			array[3].Value = model.DiscountRate;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserGiftCardFaceValue ");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string idlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserGiftCardFaceValue ");
			stringBuilder.Append(" where ID in (" + idlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public UserGiftCardFaceValueModel GetModel(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select id, FestivalName, FaceValue, DiscountRate  ");
			stringBuilder.Append("  from MBF_UserGiftCardFaceValue ");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			UserGiftCardFaceValueModel userGiftCardFaceValueModel = new UserGiftCardFaceValueModel();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			UserGiftCardFaceValueModel result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				if (dataSet.Tables[0].Rows[0]["id"] != null && dataSet.Tables[0].Rows[0]["id"].ToString() != "")
				{
					userGiftCardFaceValueModel.id = new long?(long.Parse(dataSet.Tables[0].Rows[0]["id"].ToString()));
				}
				userGiftCardFaceValueModel.FestivalName = dataSet.Tables[0].Rows[0]["FestivalName"].ToString();
				if (dataSet.Tables[0].Rows[0]["FaceValue"] != null && dataSet.Tables[0].Rows[0]["FaceValue"].ToString() != "")
				{
					userGiftCardFaceValueModel.FaceValue = new decimal?(decimal.Parse(dataSet.Tables[0].Rows[0]["FaceValue"].ToString()));
				}
				if (dataSet.Tables[0].Rows[0]["DiscountRate"] != null && dataSet.Tables[0].Rows[0]["DiscountRate"].ToString() != "")
				{
					userGiftCardFaceValueModel.DiscountRate = new int?(int.Parse(dataSet.Tables[0].Rows[0]["DiscountRate"].ToString()));
				}
				result = userGiftCardFaceValueModel;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * ");
			stringBuilder.Append(" FROM MBF_UserGiftCardFaceValue ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string fieldOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * ");
			stringBuilder.Append(" FROM MBF_UserGiftCardFaceValue ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetPagedList(int PageIndex, int PageSize, string strWhere, string Order)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.AppendFormat("select  top {0} * from [MBF_UserGiftCardFaceValue] where ", PageSize);
			if (strWhere.Trim() != "")
			{
				stringBuilder.AppendFormat(" {0} and ", strWhere);
			}
			stringBuilder.AppendFormat(" id not in (select top {0} id from [MBF_UserGiftCardFaceValue] ", (PageIndex - 1) * PageSize);
			if (strWhere.Trim() != "")
			{
				stringBuilder.AppendFormat(" where {0} ", strWhere);
			}
			if (Order.Trim() != "")
			{
				stringBuilder.Append(" order by  " + Order);
			}
			stringBuilder.Append(")");
			if (Order.Trim() != "")
			{
				stringBuilder.Append(" order by  " + Order);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
